package com.lu.jdog.util;

import java.io.*;

public class StreamUtil {
    private static final int SIZE = 1024;

    /**
     * @param is 字节输入流
     * @return 以字节流读取的字符串
     */
    public static String readToString(InputStream is) {
        return readToString(is, null);
    }

    /**
     * @param is          字节输入流
     * @param charsetName 编码方式
     * @return 以字节流读取字符串
     */
    public static String readToString(InputStream is, String charsetName) {
        String rString = null;
        int len = 0;
        byte[] bs = new byte[SIZE];

        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            while ((len = bis.read(bs)) != -1) {
                baos.write(bs, 0, len);
            }
            baos.flush();
            rString = baos.toString(charsetName);
            baos.close();
            bis.close();
            is.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return rString;
    }

    /**
     * @param is 字节输入流
     * @return 以字符流读取的字符串
     */
    public static String readCharToString(InputStream isr) {
        return readCharToString(isr, null);
    }


    /**
     * @param is          字节流
     * @param charsetName 编码方式
     * @return 以字符流读取的字符串
     */
    public static String readCharToString(InputStream is, String charsetName) {
        String rString = null;
        try {
            InputStreamReader isr = new InputStreamReader(is, charsetName);
            BufferedReader br = new BufferedReader(isr);

            CharArrayWriter caw = new CharArrayWriter();
            char[] cs = new char[SIZE];
            int len;

            while ((len = br.read(cs)) != -1) {
                caw.write(cs, 0, len);
            }
            caw.flush();
            rString = caw.toString();//与readToChars方法唯一区别之处

            caw.close();
            br.close();
            isr.close();
            is.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return rString;
    }

    /**
     * @param is 输入流
     * @return 字节数组
     */
    public static byte[] readToBytes(InputStream is) {
        byte[] rBytes = null;
        try {
            BufferedInputStream bis = new BufferedInputStream(is);

            byte[] cs = new byte[SIZE];
            int len;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((len = bis.read(cs)) != -1) {
                baos.write(cs, 0, len);
            }
            baos.flush();
            rBytes = baos.toByteArray();

            baos.close();
            bis.close();
            is.close();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rBytes;
    }

    /**
     * @param is 字节流
     * @return 字符数组
     */
    public static char[] readToChars(InputStream is) {
        return readToChars(is, null);
    }

    /**
     * @param is          字节流
     * @param charsetName 编码
     * @return 字符数组
     */
    public static char[] readToChars(InputStream is, String charsetName) {
        char[] rChars = null;
        try {
            InputStreamReader isr = new InputStreamReader(is, charsetName);
            BufferedReader br = new BufferedReader(isr);

            CharArrayWriter caw = new CharArrayWriter();
            char[] cs = new char[SIZE];
            int len;

            while ((len = br.read(cs)) != -1) {
                caw.write(cs, 0, len);
            }
            caw.flush();
            rChars = caw.toCharArray();

            caw.close();
            br.close();
            isr.close();
            is.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return rChars;
    }


}
